import 'package:flutter/material.dart';
import '../../constant/app_router.dart';
import '/common/common_util.dart';

class MinePage extends StatefulWidget {
  const MinePage({super.key});

  @override
  State<StatefulWidget> createState() => MinePageState();
}

class MinePageState extends State<MinePage> {
  String _headerTitle = "点击登录";

  @override
  void initState() {
    super.initState();
    reloadHeader();
  }

  void reloadHeader() async {
    String headerTitle = await CommonUtil.getUsername();
    setState(() {
      _headerTitle = headerTitle;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        _buildHeaderWidget(),
        const Padding(
          padding: EdgeInsets.only(top: 20),
        ),
        _buildItem("测试插件", () {
          CommonUtil.jumpToPageByPageName(
            context,
            AppRouter.plugin,
          );
        }),
        _buildItem("迷你Provider", () {
          CommonUtil.jumpToPageByPageName(
            context,
            AppRouter.provider,
          );
        }),
        _buildItem("测试自定义Layout", () {
          CommonUtil.jumpToPageByPageName(
            context,
            AppRouter.myCenterWidget,
          );
        }),
        _buildItem("关于软件", () {}),
        _buildItem("退出登录", () => logout()),
      ],
    );
  }

  Widget _buildHeaderWidget() {
    return Container(
      height: 100,
      color: Colors.white,
      padding: const EdgeInsets.all(15),
      child: InkWell(
        onTap: () {
          CommonUtil.isLogin(context, true);
        },
        child: Row(
          children: <Widget>[
            const Image(
              image: AssetImage('assets/mine/header.png'),
            ),
            const Padding(padding: EdgeInsets.only(left: 10)),
            Text(
              _headerTitle,
              style: const TextStyle(fontSize: 16, color: Colors.black),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildItem(String title, Function() onTap) {
    return Container(
      color: Colors.white,
      child: InkWell(
        onTap: onTap,
        child: Column(
          children: [
            Row(
              children: <Widget>[
                const Padding(
                  padding: EdgeInsets.fromLTRB(15, 12, 10, 12),
                  child: Image(
                    image: AssetImage("assets/mine/heart.png"),
                  ),
                ),
                Text(
                  title,
                  style: const TextStyle(fontSize: 16, color: Colors.black),
                ),
              ],
            ),
            const Divider(
              height: 1,
              color: Colors.black12,
            ),
          ],
        ),
      ),
    );
  }

  void logout() async {
    bool isLogin = await CommonUtil.isLogin(context, false);
    if (isLogin) {
      CommonUtil.removeUserInfo();
      // NetWorkUtil().GET(Api.logout, null, null, null);
      // setState(() {
      //   _headerTitle = "点击登录";
      // });
    }
  }
}
